import localForage from 'localforage';
/**
 * localForage.LOCALSTORAGE就是指定使用的存储引擎，例如 localStorage
 * localforage.INDEXEDDB就指定使用的存储引擎，例如 IndexedDB
 * localforage.WEBSQL就是
 */
export class Storage {
  private db: LocalForage;

  private projectCode: string; // 可以从外部传入，或作为构造函数参数 项目编号 
  private userEmail: string; // 同上  邮件
  
  constructor(storeName: string, projectCode: string, userEmail: string) {  
    this.projectCode = projectCode;  
    this.userEmail = userEmail;  
    this.db = localForage.createInstance({  
      driver: localForage.LOCALSTORAGE,
      name: 'fanwei', // 或者使用 ProductCodeEnum.XXX 如果已定义  
      storeName,  
      description: 'storage for 1.1.0',  
    });  
  }

  private getKey(key: string): string {
    return [key, this.projectCode, this.userEmail].join('-'); 
    // return [key].join('-'); 
  }

  getStorage() {
    return this.db;
  }

  getItem(key: string) {
    return this.getStorage().getItem(this.getKey(key));
  }

  setItem(key: string, value: unknown) {
    return this.getStorage().setItem(this.getKey(key), value);
  }

  removeItem(key: string) {
    return this.getStorage().removeItem(this.getKey(key));
  }
}

export const MylocalStorage = new Storage('DataList', 'project123', 'user@example.com');

